#-*-coding:utf-8-*-
'''
Created on 2009/6/18

@author: Hung-Hsi Chen
'''
import glob, os.path, re, time, sqlite3

def main():
    #list = glob.glob('C:stock_tmp/*.htm')
    list = glob.glob('/media/disk-1/stock_price/*.htm')
    dbname = '../../twse_stock.s3db'
    
    pat = re.compile(r'(^\d{4})_(\d{4})(\d{2}).htm', re.I)
    dbconn = sqlite3.connect(dbname)
    dbcur = dbconn.cursor()
    
    t1 = time.clock()
    for count, path in enumerate(list):
       file = os.path.split(path)
       #print file[1]
       result = pat.match(file[1])
       (stockid, year, month) = result.groups()
       sql = "insert into file_record values(NULL, '%s', '%s', '%s')"%(stockid,year, month)
       print "[%.3f]: %s"%(float(count)/len(list), sql)
#       dbcur.execute(sql)
#       dbconn.commit()
#       print "stock = %s, year = %s, month = %s"%(stockid, year, month)
    
    t2 = time.clock()
    print "total time = %.3f secs"%(t2-t1)
    
    dbcur.close()
    dbconn.close()
       
if __name__ == '__main__':
    main()